package com.app.Tree;


import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

import com.app.Activity.R;

public class MyDialog extends Dialog implements
		android.view.View.OnClickListener {
	
	private String defautTitle="[Nhập tiêu đề]";
	public static String defaultText="[Ghi chú]";
	private String titleText;// = defautTitle;
	private String text = defaultText;
	private int color;
	boolean okClick = false;
	Button btnOK, btnCancel;
	EditText txtText, txtTitle;
	MyHandlerInterface OKClickListener;
	MyHandlerInterface CancelClickListener;
	INode myObj;
	public MyDialog(Context context,String title) {
		super(context);
		this.setContentView(R.layout.menu_thao_tac_node);		
		this.setTitle(title);
		txtText = (EditText) findViewById(R.id.txtNoiDung);
		txtTitle = (EditText) findViewById(R.id.txtTitle);
		txtText.setText(this.text);
		txtTitle.setText(this.titleText);
		//txtText.clearFocus();
		//txtTitle.clearFocus();
		Spinner spColor = (Spinner) this.findViewById(R.id.spColor);
		

		final CColor[] colors = CColor.init();

		final ColorAdapter adapter = new ColorAdapter(context, R.id.spColor,
				colors);
		spColor.setAdapter(adapter);

		spColor.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				CColor c = adapter.getItem(position);
				txtText.setBackgroundColor(c.color);
				color = c.color;
				if(color==Color.BLACK){
					txtText.setTextColor(Color.WHITE);
				}else{
					txtText.setTextColor(Color.BLACK);
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}
		});
		btnCancel = (Button) findViewById(R.id.btnCancel);
		btnOK = (Button) findViewById(R.id.btnOK);

		btnCancel.setOnClickListener(this);
		btnOK.setOnClickListener(this);

		txtText.setOnFocusChangeListener(new OnFocusChangeListener() {

			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if(hasFocus){
					if (txtText.getText().toString().equals(defaultText))
						txtText.setText("");
				}else{
					
					String s=txtText.getText().toString().replace("\\s", "");
					if(s.length()==0)	{
						txtText.setText(defaultText);
					}
				}

			}
		});
		txtTitle.setOnFocusChangeListener(new OnFocusChangeListener() {

			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if (txtTitle.getText().toString().equals(defautTitle)){
					txtTitle.setText("");
				}else{
					
					String s=txtTitle.getText().toString().replace("\\s", "");
					if(s.length()==0)	{
						txtTitle.setText(defautTitle);
					}
				}

			}
		});
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		switch (id) {
		case R.id.btnOK:
			this.text = txtText.getText().toString();
			this.titleText = txtTitle.getText().toString();
			this.okClick = true;
			if(OKClickListener!=null)
		         OKClickListener.onHandle(myObj);
			break;

		default:
			this.okClick = false;
			if(CancelClickListener!=null){
				CancelClickListener.onHandle(myObj);
			}
			break;
		}
		dismiss();
	}
	
	public MyDialog(Context context,String title, String cNodeTitle) {
		super(context);
		this.setContentView(R.layout.menu_thao_tac_node);		
		this.setTitle(title);
		txtText = (EditText) findViewById(R.id.txtNoiDung);
		txtTitle = (EditText) findViewById(R.id.txtTitle);
		txtText.setText(this.text);
		txtTitle.setText(cNodeTitle);
		txtText.clearFocus();
		txtTitle.clearFocus();
		Spinner spColor = (Spinner) this.findViewById(R.id.spColor);
		

		final CColor[] colors = CColor.init();

		final ColorAdapter adapter = new ColorAdapter(context, R.id.spColor,
				colors);
		spColor.setAdapter(adapter);

		spColor.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				CColor c = adapter.getItem(position);
				txtText.setBackgroundColor(c.color);
				color = c.color;
				if(color==Color.BLACK){
					txtText.setTextColor(Color.WHITE);
				}else{
					txtText.setTextColor(Color.BLACK);
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}
		});
		btnCancel = (Button) findViewById(R.id.btnCancel);
		btnOK = (Button) findViewById(R.id.btnOK);

		btnCancel.setOnClickListener(this);
		btnOK.setOnClickListener(this);

		txtText.setOnFocusChangeListener(new OnFocusChangeListener() {

			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if(hasFocus){
					if (txtText.getText().toString().equals(defaultText))
						txtText.setText("");
				}else{
					
					String s=txtText.getText().toString().replace("\\s", "");
					if(s.length()==0)	{
						txtText.setText(defaultText);
					}
				}

			}
		});
		txtTitle.setOnFocusChangeListener(new OnFocusChangeListener() {

			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if (txtTitle.getText().toString().equals(defautTitle)){
					txtTitle.setText("");
				}else{
					
					String s=txtTitle.getText().toString().replace("\\s", "");
					if(s.length()==0)	{
						txtTitle.setText(defautTitle);
					}
				}

			}
		});
	}
	
	public MyDialog(Context context,String title, String cNodeTitle, String cNodeText) {
		super(context);
		this.setContentView(R.layout.menu_thao_tac_node);		
		this.setTitle(title);
		txtText = (EditText) findViewById(R.id.txtNoiDung);
		txtTitle = (EditText) findViewById(R.id.txtTitle);
		txtText.setText(cNodeText);
		txtTitle.setText(cNodeTitle);
		txtText.clearFocus();
		txtTitle.clearFocus();
		Spinner spColor = (Spinner) this.findViewById(R.id.spColor);
		

		final CColor[] colors = CColor.init();

		final ColorAdapter adapter = new ColorAdapter(context, R.id.spColor,
				colors);
		spColor.setAdapter(adapter);

		spColor.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				CColor c = adapter.getItem(position);
				txtText.setBackgroundColor(c.color);
				color = c.color;
				if(color==Color.BLACK){
					txtText.setTextColor(Color.WHITE);
				}else{
					txtText.setTextColor(Color.BLACK);
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}
		});
		btnCancel = (Button) findViewById(R.id.btnCancel);
		btnOK = (Button) findViewById(R.id.btnOK);

		btnCancel.setOnClickListener(this);
		btnOK.setOnClickListener(this);

		txtText.setOnFocusChangeListener(new OnFocusChangeListener() {

			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if(hasFocus){
					if (txtText.getText().toString().equals(defaultText))
						txtText.setText("");
				}else{
					
					String s=txtText.getText().toString().replace("\\s", "");
					if(s.length()==0)	{
						txtText.setText(defaultText);
					}
				}

			}
		});
		txtTitle.setOnFocusChangeListener(new OnFocusChangeListener() {

			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if (txtTitle.getText().toString().equals(defautTitle)){
					txtTitle.setText("");
				}else{
					
					String s=txtTitle.getText().toString().replace("\\s", "");
					if(s.length()==0)	{
						txtTitle.setText(defautTitle);
					}
				}

			}
		});
	}

	public void setOKClickListener(MyHandlerInterface oKClickListener) {
		OKClickListener = oKClickListener;
	}

	public void setCancelClickListener(MyHandlerInterface cancelClickListener) {
		CancelClickListener = cancelClickListener;
	}

	public String getTextTitle() {
		return titleText;
	}

	public void setTextTitle(String title) {
		this.titleText = title;
		this.txtTitle.setText(title);
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
		this.txtText.setText(text);
	}

	public int getColor() {
		return color;
	}

	public void setColor(int color) {
		this.color = color;
	}

	public boolean isOkClick() {
		return okClick;
	}

	public void setOkClick(boolean okClick) {
		this.okClick = okClick;
	}

}
